Client Script In Service-now

    1.To alert fullname of user:

    function onLoad() {

           var p=g_user.getFullName();

        alert('This is my first client Script= '+p);

    }


    2.To alert If the Record is new or not:

    function onLoad() {

        if (g_form.isNewRecord()){

            alert('This is new Record');

        }

        else{

            alert('This is not a new record');

        }

    }


    3.Use of different API's:

    function onLoad() {

        g_form.setReadOnly('u_email',true);

        g_form.setValue('u_description','hiiiiiii');

          g_form.clearOptions('u_category');

        g_form.getLabelOf('u_number');

        g_form.addInfoMessage('hi');

        g_form.setMandatory('u_first_name',true);

        g_form.setMandatory('u_last_name',true);

        if (g_user.hasRole(admin)){

        g_form.removeOption('u_state','Maharashtra');

        }

    }


    4.To make field visible after creating record but not while creating Record


    function onLoad() {

      

       if(g_form.isNewRecord()){

    g_form.setVisible("u_convert_to",false);

    }

        else {

            g_form.setVisible("u_convert_to",true);

        }

    }


     

    5.when Assigned To value changes, populte mail id of changed user in email field and populate user name in short description


    function onChange(control, oldValue, newValue, isLoading, isTemplate) {

       if (isLoading || newValue === '') {

          return;

       }

    var p=g_form.getReference("u_assigned_to").email;

        g_form.setValue("u_email",p);

        g_form.setMandatory("u_first_name",true);

        g_form.setValue("u_short_description",'This is :'+g_user.getFullName());

    }


    6.when Assigned To value changes,populate user's first name in short description


    function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {

    return;

      }

    var p = g_form.getValue("u_assigned_to");

        var name= g_form.getReference("u_assigned_to");

        g_form.setValue("u_short_description", 'This is'+name.first_name);

        

    alert(newValue);

    alert(oldValue);   

       

    }


    7.Confirm details before submit form

    function onSubmit() {

       //Type appropriate comment here, and begin script below

       var a = g_form.getValue("u_first_name");

        var b = g_form.getValue("u_last_name");

        var con= confirm( 'You Entered below datails-\n The first name is- '+a+'\n The last name is- '+b+'\n  Do you confirm the details?' );

        if(con==true){

            return;

        }

        else{

            return 0;

        }

    }


    8. On Submit

    function onSubmit() {

       //Type appropriate comment here, and begin script below

        var p = g_form.getValue("u_assigned_to");

        var name= g_form.getReference("u_assigned_to");

        g_form.setValue("u_short_description", name.first_name);

    alert(p);


    9. On editing first name in list view (onCellEdit Client Script)

    function onCellEdit(sysIDs, table, oldValues, newValue, callback) {

      var saveAndClose = true;

     

        alert(sysIDs);

        alert(table);

        alert( oldValues);

        alert(newValue);

        alert(callback);

     

     callback(saveAndClose); 

    }